<%@page import="models.Barang" %>
<%@page import="java.util.ArrayList" %>
<%@page import="models.Kategori" %>
<% Barang obj = (Barang) request.getAttribute("object");%>
<% ArrayList<Kategori> objKategori = (ArrayList<Kategori>) request.getAttribute("listKategori");%>

<script type="text/javascript">
    $(document).ready(function() {
        $("#updateBarangForm").validate({
            rules : {
                nama_barang : {
                    required : true
                },
                harga_satuan : {
                    required : true,
                    number : true
                },
                jumlah : {
                    required : true,
                    number : true
                },
                poin : {
                    required : true,
                    number : true
                },
                deskripsi : {
                    required : true
                }
            },
            messages : {
                nama_barang : "x",
                harga_satuan : "x",
                jumlah : "x",
                deskripsi : "x"
            }
        });
        $("#updatePhotoForm").validate({
            rules : {
                path_gambar : {
                    required : true
                }
            },
            messages : {
                path_gambar : "x"
            }
        });
    });

    function Show_Popup(layout) {
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupWidth = $('#' + layout).width();
        var popupHeight = $('#' + layout).height();
        $('#'+layout).css({
            "position": "fixed",
            "top": (windowHeight / 2 - popupHeight / 2),
            "left": (windowWidth / 2 - popupWidth / 2)
        });
        $('#'+layout).fadeIn('fast');
    }
    
    function Close_Popup(layout) {
        $('#'+layout).fadeOut('fast');
    }
</script>

<h1>Ubah Barang</h1>
<form id="updateBarangForm" action="<%=request.getContextPath()%>/barang/update" method="POST">
    <input type="hidden" name="id" value="<%= obj != null ? obj.id : ""%>">
    <input type="hidden" name="path_gambar" value="<%= obj != null ? obj.path_gambar : ""%>">
    <table>
        <tr>
            <td><label>Gambar</label></td>
            <td><img alt="" class="item-image" src="<%= request.getContextPath() + "/" + (obj != null ? obj.path_gambar : "")%>" onclick="javascript:Show_Popup('updatePhoto');"></td></tr>
        <tr>
            <td><label>Kategori</label></td>
            <td>
                <select name="kategori">
                    <% for (int i = 0; i < objKategori.size(); i++) {%>
                    <option value="<%= objKategori.get(i).id%>" <% if (objKategori.get(i).id == obj.kategori_id) {
                             out.print("selected");
                         }%>><%= objKategori.get(i).nama%></option>
                    <% }%>
                </select>
            </td>
        </tr>
        <tr><td><label>Nama Barang</label></td><td><input type="text" name="nama_barang" value="<%= obj != null ? obj.nama : ""%>" /></td></tr>
        <tr><td><label>Harga Satuan</label></td><td><input type="text" name="harga_satuan" value="<%= obj != null ? obj.harga_satuan : ""%>" /></td></tr>
        <tr><td><label>Stock</label></td><td><input type="text" name="jumlah" value="<%= obj != null ? obj.jumlah : ""%>" /></td></tr>
        <tr><td><label>Poin</label></td><td><input type="text" name="poin" value="<%= obj != null ? obj.poin : ""%>"  /></td></tr>
        <tr><td><label>Berat</label></td><td><input type="text" name="berat" value="<%= obj != null ? obj.berat : ""%>"  /></td></tr>
        <tr><td><label>Deskripsi</label></td><td><textarea name="deskripsi"><%= obj != null ? obj.deskripsi : ""%></textarea></td></tr>
        <tr><td></td><td><input type="submit" value="Update" /></td></tr>
    </table>
</form>
<div id="updatePhoto" style="display:none">
    <form id="updatePhotoForm" action="<%=request.getContextPath()%>/barang/update/image?id=<%= obj != null ? obj.id : ""%>" method="POST" enctype="multipart/form-data">
        <table>
            <tr><td>Image File : </td><td><input type="file" name="path_gambar"/></td></tr>
            <tr><td></td><td><input type="submit" value="Update" /></td></tr>
        </table>
    </form>
</div>